home *** CD-ROM | disk | FTP | other *** search
/ Belgian Amiga Club - ADF Collection / BS1 part 41.zip / BS1 part 41 / Lattice C v5.02 d4.adf / examples / avail.c next >
C/C++ Source or Header  |  1988-11-07  |  5KB  |  139 lines

  1. ; /*
  2. lc -v -j73 -cus -M -O avail
  3. blink avail.o nd
  4. protect avail +p
  5. quit
  6. */
  7.  
  8. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  9. /* |_o_o|\\ Copyright (c) 1988 The Software Distillery.  All Rights Reserved */
  10. /* |. o.| || This program may not be distributed without the permission of   */
  11. /* | .  | || the authors:                                          BBS:      */
  12. /* | o  | ||   Gordon Keener    John Toebes                  (919)-471-6436  */
  13. /* |  . |//                                                                  */
  14. /* ======                                                                    */
  15. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  16. /*                                                                           */
  17. /* Avail.c - Remotely related to the original version found on 1.2 Gamma     */
  18. /*           Toolkit disk.                                                   */
  19. /*                                                                           */
  20. /*  To compile, simply execute the C program as an execute script            */
  21. /*    SHAR eat your heart out...                                             */
  22. /*                                                                           */
  23. /*  Requires Lattice 5.0 and does not use any assembler startup...           */
  24. /*   Runs ONLY from CLI, accepts command line arguments EXACTLY like the 1.3   */
  25. /*   version.                                                                */
  26. /*                                                                           */
  27. /*  In doing this we might have just broken every rule in the book as well   */
  28. /*   as made up several new tricks along the way.  But then again, what can  */
  29. /*   you say for a 604 byte RESIDENT module COMPLETELY written in C?         */
  30. /*                                                                           */
  31. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  32.  
  33. #include <exec/types.h>
  34. #include <exec/exec.h>
  35. #include <exec/execbase.h>
  36. #include <libraries/dos.h>
  37. #include <proto/exec.h>
  38. #include <proto/dos.h>
  39. #include <string.h>
  40.  
  41. void RawDoFmt(char *, long *, void (*)(), char *);
  42. #pragma syscall RawDoFmt 20a ba9804
  43.  
  44. void __regargs prbuf(char c);
  45. #define R_A3 (8+3)
  46.  
  47. #define ABSEXECBASE ((struct ExecBase **)4L)
  48.  
  49. void __asm avail(register __a0 char *cmd)
  50. {
  51.    struct MemHeader  *mem;
  52.    long              *pptr;
  53.    ULONG              max1, max2;
  54.    ULONG              avail1, avail2;
  55.    ULONG              largest1, largest2;
  56.    char               obuf[200];
  57.    long               parray[13];
  58.    char               c;
  59.    struct DosLibrary *DOSBase;
  60.    char              *txt;
  61.  
  62.    while((c = (*cmd++|0x20)) == ' ');
  63.    max1 = max2 = 0;
  64.  
  65.    if (!(DOSBase = (struct DosLibrary *)OpenLibrary("dos.library",0)))
  66.       return;
  67.  
  68.    Forbid ();
  69.    for (mem = (struct MemHeader *)(*ABSEXECBASE)->MemList.lh_Head;
  70.         mem->mh_Node.ln_Succ;
  71.         mem = (struct MemHeader *)mem->mh_Node.ln_Succ)
  72.       {
  73.       if (mem -> mh_Attributes & MEMF_CHIP)
  74.          max1 += ((ULONG) mem -> mh_Upper - (ULONG) mem -> mh_Lower);
  75.       /* Just in case we have something that is both chip and fast... */
  76.       if (mem -> mh_Attributes & MEMF_FAST)
  77.          max2 += ((ULONG) mem -> mh_Upper - (ULONG) mem -> mh_Lower);
  78.       }
  79.  
  80.    avail1   = AvailMem (MEMF_CHIP);
  81.    largest1 = AvailMem (MEMF_CHIP | MEMF_LARGEST);
  82.  
  83.    avail2   = AvailMem (MEMF_FAST);
  84.    largest2 = AvailMem (MEMF_FAST | MEMF_LARGEST);
  85.    Permit ();
  86.  
  87.    pptr = parray;
  88.    *pptr++ = avail1;
  89.  
  90.    txt = "%ld\n";
  91.  
  92.    if (c == '?')
  93.       txt = "Usage: avail [CHIP|FAST|TOTAL]\n";
  94.    else if (c == 't')
  95.       parray[0] += avail2;
  96.    else if (c == 'f')
  97.       parray[0] = avail2;
  98.    else if (c != 'c')
  99.       {
  100.       /*        ssss   dddddddd  dddddddd  dddddddd  dddddddd  */
  101.       txt =  "Type  Available    In-Use   Maximum   Largest\n"
  102.              "chip%11ld %9ld %9ld %9ld\n"
  103.          "fast%11ld %9ld %9ld %9ld\n"
  104.              "total%10ld %9ld %9ld %9ld\n";
  105.       *pptr++ = max1-avail1;
  106.       *pptr++ = max1;
  107.       *pptr++ = largest1;
  108.  
  109.       *pptr++ = avail2;
  110.       *pptr++ = max2-avail2;
  111.       *pptr++ = max2;
  112.       *pptr++ = largest2;
  113.       
  114.       *pptr++ = avail1+avail2;
  115.       *pptr++ = (max1+max2)-(avail1+avail2);
  116.       *pptr++ = max1+max2;
  117.       max1 = largest1;
  118.       if (largest2 > max1) max1 = largest2;
  119.       *pptr++ = max1;
  120.       }
  121.  
  122.    RawDoFmt(txt, parray, prbuf, obuf);
  123.    Write(Output(), obuf, strlen(obuf));
  124.    CloseLibrary((struct Library *)DOSBase);
  125. }
  126.  
  127. /*----------------------------------------------------------------*/
  128. /* This stub routine is called from the RawDoFmt routine for each */
  129. /* character in the string.  At invocation, we have:              */
  130. /*   D0 - next character to be formatted                          */
  131. /*   A3 - pointer to data buffer                                  */
  132. /*----------------------------------------------------------------*/
  133. void __regargs prbuf(char c)
  134. {
  135. char *p = (char *)__builtin_getreg(R_A3);
  136. *p++ = c;
  137. __builtin_putreg(R_A3, p);
  138. }
  139.